#include "Utility.h"

// Converts an integer ip address to the dot form.
void get_ip_from_int(uint32_t ip, char* buffer) {
	uint8_t bits[4];
	int i;
	for(i=0;i<4;i++) {
		bits[i] = (ip >> (i*8)) & 0xFF;
	}
	sprintf(buffer,"%u.%u.%u.%u",bits[3],bits[2],bits[1],bits[0]);
}

// Host to network on a 64 bit int
uint64_t hton64b(uint64_t in) {
	if(htons(1)==1)
		return in;
	char* x=(char*)&in;
	int i;
	for(i=0;i<4;i++) {
		char tmp=x[i];
		x[i]=x[7-i];
		x[7-i]=tmp;
	}
	return in;
}

// Network to host on a 64 bit int
uint64_t ntoh64b(uint64_t in) {
	if(ntohs(1)==1)
		return in;
	char* x=(char*)&in;
	int i;
	for(i=0;i<4;i++) {
		char tmp=x[i];
		x[i]=x[7-i];
		x[7-i]=tmp;
	}
	return in;
}
